#include <cstdio>
#include <iostream>
#include <csignal>

//const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = "efs";


#include "common/log.hpp"
#include "common/Utils.hpp"
#include "common/Settings.hpp"
#include "folder-manager/FolderManager.hpp"
#include "folder-manager/FolderDescription.hpp"

#include "common/XmlUtils.hpp"

using namespace efs;
using namespace rapidxml;

rapidxml::xml_node<> *prepareXML(rapidxml::xml_document<> &xml)
{
	xml.clear();
	xml_node<>* decl = xml.allocate_node(node_declaration);
	decl->append_attribute(xml.allocate_attribute("version", "1.0"));
	decl->append_attribute(xml.allocate_attribute("encoding", "utf-8"));
	xml.append_node(decl);
	xml_node<> *root = xml.allocate_node(node_element, "root");
	xml.append_node(root);
	return root;
}

int main(int argc, char *argv[])
{
	Settings settings;
	FolderManager folder_manager("test_folder_1/", 1, settings);
	FolderDescription folder_description("test_folder_1/");
	FolderDescription folder_description2("test_folder_2/");

	folder_description.scanFolder();
	folder_description.print();

	char q = ' ';
	while (q != 'q')
	{
		std::cin >> q;
		pantheios::log_DEBUG("");
		bool f1 = folder_description.scanFolder();
		bool f2 = folder_description2.scanFolder();
		if (q == 'p')
		{
			std::cout << std::endl << "folder 1: " << f1 << std::endl;
			folder_description.print();
			std::cout << "folder 2:" << f2 << std::endl;
			folder_description2.print();
		}

		if (q == 'c')
		{
			std::vector<FileDescription> get_list;
			std::vector<FileDescription> remove_list;

			folder_description.scanFolder();
			folder_description2.scanFolder();
			folder_description.compareExternal(folder_description2.getFolderDescription(), get_list, remove_list);

			std::cout << std::endl << "get list:" << std::endl;
			for (int i = 0; i != get_list.size(); ++i)
			{
				get_list[i].print();
			}

			std::cout << std::endl << "remove list:" << std::endl;
			for (int i = 0; i != remove_list.size(); ++i)
			{
				remove_list[i].print();
			}
		}

		if (q == 'x')
		{
			rapidxml::xml_document<> xml;
			folder_description.toXML(xml, prepareXML(xml));
			std::cout << XmlToString(xml) <<  std::endl;
			folder_description.fromXML(xml, xml.first_node("root"));
			folder_description.print();
		}

	}


}

